package plot
{
	import mx.collections.ArrayCollection;
	import mx.collections.Sort;
	
	public class Utils
	{
		public function Utils()
		{
		}
		
		/////////////////////////////////////////////////////////////////////
        // Auxilary functions
        /////////////////////////////////////////////////////////////////////
        
        /**
        * Sorts the object in lexicographic order
        */
        public static function sortMap(map:Object, type:Class):Object{
        	if (map == null) return map;
        	var current:Object;
        	// copy all obj to collection
        	var collection:ArrayCollection = new ArrayCollection();
        	for (var i:String in map){
        		collection.addItem(map[i]);
        	}
        	// sort obj in collection
        	var sort:Sort = new Sort();
        	if (type == Date)
        		sort.compareFunction = compareDates;
        	else if (type == String)
        		sort.compareFunction = compareStrings;	
        	collection.sort = sort;
        	collection.refresh();
        	// convert collection back to object
        	var new_map:Object = new Object();
        	for (var j:int = 0; j < collection.length; j++)
        		new_map[collection[j]] = j;
        	
        	return new_map;
        }
        /**
        * Compares two objects. If objects are both Dates, returns -1 if a < b,
        * 0 if they are euqal, and 1 otherwise.
        * If both objects are Strings, returns same as Java's a.compare(b);
        * If objects are neither Dates, nor strings, throws a casting exception.
        */
        public static function compareDates(a:Object, b:Object, fields:Array = null):int{
        		if ((a as Date) < (b as Date)) return -1;
        		else if ((a as Date) == (b as Date)) return 0;
        		else return 1; 
        }
        public static function compareStrings(a:Object, b:Object, fields:Array = null):int{
        		if (a < b) return -1;
        		else if (a == b) return 0;
        		else return 1;
        }

	}
}